package xdpm.nhom16.DAO;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySQLiteHelper extends SQLiteOpenHelper
{

	private static int DATABASE_VERSION = 1;
	private static String DATABASE_PATH = "/data/data/xdpm.nhom16/";
	private static String DATABASE_NAME = "DanhSachDichVu";
	private final Context myContext;
	private SQLiteDatabase myDataBase;

	public SQLiteDatabase getMyDataBase()
	{
		return myDataBase;
	}

	public MySQLiteHelper(Context context)
	{
		super(context, DATABASE_PATH + DATABASE_NAME, null, DATABASE_VERSION);
		this.myContext = context;
		try
		{
			createDatabase(myContext);

		}
		catch (IOException ioe)
		{
			throw new Error("Unable to create database");
		}
		
	}

	// public MySQLiteHelper(Context context, String name, CursorFactory factory, int version)
	// {
	// super(context, name, factory, version);
	// }

	public void createDatabase(Context myContext ) throws IOException
	{
		boolean dbExist = checkDataBase();

		if (dbExist)
		{

		}
		else
		{
			try
			{
				copyDatabase(myContext);
			}
			catch (IOException e)
			{
				throw new Error("Error copying database");
			}
		}
	}

	private static boolean checkDataBase()
	{
		try
		{
			String myPath = DATABASE_PATH + DATABASE_NAME;

			File f = new File(myPath);

			if (f.exists())
			{
				return true;
			}
		}
		catch (SQLException e)
		{
			return false;
		}

		return false;
	}

	private void copyDatabase(Context myContext) throws IOException
	{
		final int BUFFER = 2048;

		try
		{
			BufferedOutputStream dest = null;
			InputStream is = myContext.getAssets().open(DATABASE_NAME + ".zip");
			ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
			ZipEntry entry;

			while ((entry = zis.getNextEntry()) != null)
			{
				int count;
				byte data[] = new byte[BUFFER];

				FileOutputStream fos = new FileOutputStream(DATABASE_PATH + entry.getName());
				dest = new BufferedOutputStream(fos, BUFFER);
				while ((count = zis.read(data, 0, BUFFER)) != -1)
				{
					dest.write(data, 0, count);
				}
				dest.flush();
				dest.close();
			}
			zis.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	// public void openDataBase() throws SQLException
	// {
	// String myPath = DATABASE_PATH + DATABASE_NAME;
	// myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
	// }

	// @Override
	// public synchronized void close()
	// {
	//
	// if (myDataBase != null)
	// myDataBase.close();
	//
	// super.close();
	//
	// }

	@Override
	public void onCreate(SQLiteDatabase db)
	{

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{

	}

	// Add your public helper methods to access and get content from the database.
	// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
	// to you to create adapters for your views.

}
